import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myflutterproject/utils/toast.dart';

class DragTargetDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DragTarget'),
        centerTitle: true,
      ),
      body: Container(
        //double.infinity  和父控件一样大
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            // 使用，如果有三个及以上的子组件时，建议使用 Positioned 包裹子组件来决定子组件的位置，alignment 的可选值有：
            Positioned(
                top: 10,
                left: 10,
                child: Draggable(
                  data: 'aa',
                  child: Container(
                    color: Colors.blue,
                    width: 100,
                    height: 100,
                    child: Text('aa'),
                  ),
                  feedback: Container(
                    color: Colors.red,
                    width: 100,
                    height: 100,
                    child: Text('aa'),
                  ),
                )),
            Positioned(
                top: 10,
                right: 10,
                child: Draggable(
                  data: 'bb',
                  child: Container(
                    color: Colors.blue,
                    width: 100,
                    height: 100,
                    child: Text('bb'),
                  ),
                  feedback: Container(
                    color: Colors.red,
                    width: 100,
                    height: 100,
                    child: Text('bb'),
                  ),
                )),
            Positioned(
              child: DragTarget(
                builder: (BuildContext context, List<String?> candidateData,
                    List<dynamic> rejectedData) {
                  print('DragTarget builder');
                  Color c;
                  String s;
                  if (candidateData.isNotEmpty && candidateData.first == 'aa') {
                    c = Colors.amber;
                    s = '接收到aa';
                  } else if (rejectedData.isNotEmpty) {
                    c = Colors.grey;
                    s = '未接收到aa，接收为${rejectedData.first}';
                  } else {
                    s = '只接收aa';
                    c = Colors.cyan;
                  }

                  return Container(
                    color: c,
                    width: 100,
                    height: 100,
                    child: Text(s),
                  );
                },
                onWillAccept: (s) {
                  print('onWillAccept $s');
                  if (s == 'aa') {
                    return true;
                  } else {
                    return false;
                  }
                },
                onAccept: (s) {
                  print('onAccept $s');
                  // Toast.toast('成功接收到指定数据$s');
                },
                onAcceptWithDetails: (DragTargetDetails<String> details) {
                  print('onAcceptWithDetails ${details.data}');
                  print('onAcceptWithDetails ${details.offset}');
                },
                onLeave: (s) {
                  print('onLeave $s');
                },
                onMove: (DragTargetDetails<dynamic> details) {
                  print('onMove ${details.data}');
                  print('onMove ${details.offset}');
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}